package com.binfenhulian.chinaenergy.ces.crawler.pipeline.impl;

import com.alibaba.fastjson.JSON;
import com.binfenhulian.chinaenergy.ces.crawler.bean.param.CrawlerQueueElement;
import com.binfenhulian.chinaenergy.ces.crawler.constants.Constants;
import com.binfenhulian.chinaenergy.ces.crawler.pipeline.PersistentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wanghjbuf on 2018/3/12.
 */
@Slf4j
@Component("com.binfenhulian.chinaenergy.ces.crawler.pipeline.impl.RedisListServiceImpl")
public class RedisListServiceImpl implements PersistentService {

    @Resource(name = "defRedisTemplate")
    RedisTemplate<String, Object> redisTemplate;
    @Autowired
    Constants constants;

    /**
     * 执行存储
     * @param resultItems
     */
    @Override
    public void doStore(ResultItems resultItems) {

        String crawler_no = resultItems.get("crawler_no");
        List<String> urls = resultItems.get("list_url");
        List<String> contents = resultItems.get("list_content");

        StringBuffer queueKey = new StringBuffer(constants.CRAWLER_REDIS_SEED_URL_QUEUE);
        List<String> lpushList = new ArrayList<>();
        for (int i=0; i<urls.size(); i++) {
            CrawlerQueueElement crawlerQueueElement = new CrawlerQueueElement();
            crawlerQueueElement.setCrawlerNo(crawler_no);
            crawlerQueueElement.setUrl(urls.get(i));
            crawlerQueueElement.setContent(contents.get(i));

            lpushList.add(JSON.toJSONString(crawlerQueueElement));
        }
        redisTemplate.opsForList().leftPush(queueKey.toString(), (String[])lpushList.toArray());
    }

}
